package cn.sfturing.web;

import cn.sfturing.entity.DoctoWork;
import cn.sfturing.service.DoctoWorkService;
import cn.sfturing.utils.StringAndDate;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value="/order")
public class DoctoWorkController {

    @Autowired
    DoctoWorkService doctoWorkService;

    /**
     * 根据医生id和日期查找数据
     */
    @RequestMapping(value = "/getOrderInfo ", method = RequestMethod.POST)
    @ResponseBody
    public Object login( @RequestParam("doctorId")Integer doctorId, @RequestParam("startDate") String startDate, @RequestParam("endDate")String endDate){
        Map map = new HashMap<String, Object>();
        List<DoctoWork> add = doctoWorkService.selecrByIdAndDate(doctorId, startDate, endDate);
        //求天数
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        try {
            //转换的过程中可能会抛出ParseException，必须抛出或者捕获处理
            start = dateFormat.parse(startDate);
            end = dateFormat.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int days = (int) ((end.getTime() - start.getTime()) / (1000*3600*24)+1);

        List<DoctoWork> add1 = new ArrayList<>();
        // 得到开始时间
        String date = startDate;
        // 循环days天
        for(int i=0;i<days;i++) {
            int t=0;
            // 循环查询出来的数组，把时间吻合的放入返回数组
            if (null != add){
                for (DoctoWork doctoWork : add) {
                    if(doctoWork.getWorkDate().equals(date)){
                        add1.add(doctoWork);
                        t=1;
                    }
                }
            }
            //没有话新建一个空白数组
            if(t==0){
                DoctoWork doctoWork = new DoctoWork();
                doctoWork.setId(i);
                doctoWork.setDoctorId(doctorId);
                doctoWork.setTimeS("0");
                doctoWork.setTimeZ("0");
                doctoWork.setTimeX("0");
                doctoWork.setWorkDate(date);
                doctoWork.setGmtCreate(new Date());
                doctoWork.setGmtModified(new Date());
                add1.add(doctoWork);
            }
            //将初始时间转化为Date加一天
            Date date1 = new StringAndDate().getDate(date);
            date=dateFormat.format(date1.getTime()+(1000*3600*24));
        }
        map.put("doctorWorkList", add1);
        return JSONArray.toJSONString(map);
    }

    //管理员修改预约时段
    @RequestMapping(value = "/admin/updateWork")
    @ResponseBody
    public Object updateWork(DoctoWork doctoWork){
        //数据处理
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(doctoWorkService.updateByIdAndDate(doctoWork)){
            resultMap.put("addResult", "true");
        }else {
            resultMap.put("addResult", "false");
        }
        return JSONArray.toJSONString(resultMap);
    }

}
